<?php

namespace yifang\model\concern;

use yifang\model\Model;
use yifang\db\BaseQuery as Query;

trait SoftDelete
{
    public function db($scope = [])
    {
        $query = parent::db($scope);
        $this->withNoTrashed($query);
        return $query;
    }

    /**
     * 判断当前实例是否被软删除
     * @return bool
     */
    public function trashed()
    {
        $field = $this->getDeleteTimeField();
        if ($field && !empty($this->getOrigin($field))) {
            return true;
        }
        return false;
    }

    public function scopeWithTrashed(Query $query)
    {
        $query->removeOption('soft_delete');
    }

    public function scopeOnlyTrashed(Query $query)
    {
        $field = $this->getDeleteTimeField(true);
        if ($field) {
            $query->useSoftDelete($field, $this->getWithTrashedExp());
        }
    }

    /**
     * 获取软删除数据的查询条件
     * @return array
     */
    protected function getWithTrashedExp()
    {
        return is_null($this->defaultSoftDelete) ? ['notnull', ''] : ['<>', $this->defaultSoftDelete];
    }

    /**
     * 删除当前的记录
     * @return bool
     */
    public function delete()
    {
        if (!$this->isExists() || $this->isEmpty() || false === $this->trigger('BeforeDelete')) {
            return false;
        }
        $name  = $this->getDeleteTimeField();
        $force = $this->isForce();
        if ($name && !$force) {
            // 软删除
            $this->set($name, $this->autoWriteTimestamp());
            $this->exists()->withEvent(false)->save();
            $this->withEvent(true);
        } else {
            // 读取更新条件
            $where = $this->getWhere();
            // 删除当前模型数据
            $this->db()
                ->where($where)
                ->removeOption('soft_delete')
                ->delete();

            $this->lazySave(false);
        }
        // 关联删除
        if (!empty($this->relationWrite)) {
            $this->autoRelationDelete($force);
        }
        $this->trigger('AfterDelete');
        $this->exists(false);
        return true;
    }

    /**
     * 删除记录
     * @return bool
     */
    public static function destroy($data, $force = false)
    {
        // 传入空值（包括空字符串和空数组）的时候不会做任何的数据删除操作，但传入0则是有效的
        if (empty($data) && 0 !== $data) {
            return false;
        }
        $model = (new static());
        $query = $model->db(false);
        // 仅当强制删除时包含软删除数据
        if ($force) {
            $query->removeOption('soft_delete');
        }
        if (is_array($data) && key($data) !== 0) {
            $query->where($data);
            $data = null;
        } elseif ($data instanceof \Closure) {
            call_user_func_array($data, [&$query]);
            $data = null;
        } elseif (is_null($data)) {
            return false;
        }
        $resultSet = $query->select($data);
        foreach ($resultSet as $result) {
            /** @var Model $result */
            $result->force($force)->delete();
        }
        return true;
    }

    /**
     * 恢复被软删除的记录
     * @return bool
     */
    public function restore($where = [])
    {
        $name = $this->getDeleteTimeField();
        if (!$name || false === $this->trigger('BeforeRestore')) {
            return false;
        }
        if (empty($where)) {
            $pk = $this->getPk();
            if (is_string($pk)) {
                $where[] = [$pk, '=', $this->getData($pk)];
            }
        }
        // 恢复删除
        $this->db(false)
            ->where($where)
            ->useSoftDelete($name, $this->getWithTrashedExp())
            ->update([$name => $this->defaultSoftDelete]);
        $this->trigger('AfterRestore');
        return true;
    }

    /**
     * 获取软删除字段
     * @return string|false
     */
    protected function getDeleteTimeField($read = false)
    {
        $field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ? $this->deleteTime : 'delete_time';

        if (false === $field) {
            return false;
        }
        if (false === strpos($field, '.')) {
            $field = '__TABLE__.' . $field;
        }
        if (!$read && strpos($field, '.')) {
            $array = explode('.', $field);
            $field = array_pop($array);
        }
        return $field;
    }

    /**
     * 查询的时候默认排除软删除数据
     */
    protected function withNoTrashed(Query $query)
    {
        $field = $this->getDeleteTimeField(true);
        if ($field) {
            $condition = is_null($this->defaultSoftDelete) ? ['null', ''] : ['=', $this->defaultSoftDelete];
            $query->useSoftDelete($field, $condition);
        }
    }

}